﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.XPath;

using HtmlToXml;
using System.Web;

namespace VKlibs
{
    public class Audiolist : List<Audio>
    {
        public Audiolist(WebBrowser webBrowser)
        {
            // Preparing navigator
            String html = webBrowser.Document.Body.InnerHtml;
            XmlDocument doc = XmlUtil.CreateXmlDocumentFromHtml(html);
            XPathNavigator navigator = doc.CreateNavigator();
            // Selecting audio
            foreach (XPathNavigator nav in navigator.Select(".//TD[contains(@class,'play_btn')]/.."))
            {
                String name = nav.SelectSingleNode(".//TD[@class='audio_desc']/DIV[1] | .//DIV[@class='audioTitle']").Value;
                name = XmlUtil.AdvancedTrim(name);
                String url = nav.SelectSingleNode(".//TD[contains(@class,'play_btn')]/IMG/@onclick").Value;
                url = url.Substring(url.IndexOf(',') + 1);
                url = url.Substring(0, url.IndexOf(','));
                if (!String.IsNullOrEmpty(name) && !String.IsNullOrEmpty(url))
                {
                    Audio audio = new Audio(name, url);
                    this.Add(audio);
                }
            }
        }
    }
}
